const Koa = require('koa')
const app = new Koa()
const mongoose = require('mongoose')
const {connect , initSchemas} = require('./database/init.js')

//解决跨域
const cors = require('koa2-cors')
app.use(cors());

//链接数据库，前台和后台的通讯 
const bodyParser = require('koa-bodyparser')
app.use(bodyParser());

//引入路由
const Router = require('koa-router')
let user = require('./appApi/User.js')
let goods = require('./appApi/goods.js')
//装载所有子路由
let router = new Router()
router.use('/user',user.routes())
router.use('/goods',goods.routes())
// 加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())




;(async()=>{
    await connect()
    initSchemas()
    
    // const User = mongoose.model('User')
    // let onUser = new User({userName:"chengxiang04",password:'123456'})
    // onUser.save().then(()=>{
    //     console.log("插入成功")
    // })

    //读出插入的数据
    // let users = await User.find()
    // console.log("---------------")
    // console.log(users)
    // console.log("---------------")
})()

app.use(async function(ctx){
    ctx.body="<h1>你好</h1>"
})

app.listen(3000,()=>{
    console.log('OK')
})